package io.dcloud.uniplugin;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.MifareUltralight;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import io.dcloud.uniplugin.bean.MessageEvent;
import io.dcloud.uniplugin.dialogfragment.CommonBasicDialog;
import io.dcloud.uniplugin.listener.NFCListener;
import io.dcloud.uniplugin.nfc.NFCInitTools;
import io.dcloud.uniplugin.tools.NfcVUtil;
import io.dcloud.uniplugin.tools.ThreadTools;


/**
 * author : yzhg
 * 包名: com.benben.nfclib
 * 时间: 2024-09-13 15:27
 * 描述：
 */
public class NFCHomeActivity extends Activity {
    private NfcAdapter mNfcAdapter;

    private IntentFilter[] mIntentFilter;

    private PendingIntent mPendingIntent;

    private String[][] mTechList;


    private boolean isDebug = NFCInitTools.isDebug;


    private CommonBasicDialog recognition;

    private boolean factory = false;
    private boolean isWrite = false; //是否写入



    private JSONObject data;
    private String mLanguage = "zh";

    /*获取到的model和tagId*/
    private String checkModel;
    private String checkTagId;

    //接收NFC指令的次数
    private int RECEIVE_NFC_POSITION = 0;


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void message(MessageEvent<Object> event) {
        if (event.getType() == MessageEvent.WRITE_MESSAGE) {
            JSONObject jsonObject = event.getJsonObject();
            acceptParams(jsonObject);
        }
        if (event.getType() == MessageEvent.WRITE_MESSAGE_TWO) {
            isWrite = true;
            JSONObject jsonObject = event.getJsonObject();
            data = jsonObject;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (isDebug) {
            setContentView(R.layout.activity_home_nfc_layout);
            //写入数据
            findViewById(R.id.butClick).setOnClickListener(view -> {
                JSONObject jsonObject = new JSONObject();
                List<String> list = new ArrayList<>();
                for (int i = 0; i < 8; i++) {
                    list.add("15");
                }
//                jsonObject.put("grade1", list);
                jsonObject.put("grade", "01");
                acceptParams(jsonObject);
                // NFCInitTools.writeTag2(arrayOf, 27, getIntent(), mNfcAdapter);
            });

            //设置秘钥
            byte[] bytes = NfcVUtil.hexStringToByte("1AAF3587100153E0");
            byte[] newBytes = NfcVUtil.hexStringToByte("00000000");
            findViewById(R.id.butSetPassword).setOnClickListener(v -> {
                NFCInitTools.writePsw(newBytes, bytes, getIntent(), mNfcAdapter, mLanguage, new NFCListener() {
                    @Override
                    public void writePswSuccess(boolean b) {
                        Toast.makeText(NFCHomeActivity.this, "写入秘钥成功" + b, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void writePswError() {
                        Toast.makeText(NFCHomeActivity.this, "写入秘钥失败", Toast.LENGTH_SHORT).show();
                    }
                });
            });

            findViewById(R.id.butCheckPassword).setOnClickListener(v -> {
                //校验秘钥
                NFCInitTools.setPsw(NfcVUtil.hexStringToByte("12345678"), getIntent(), mNfcAdapter, mLanguage, new NFCListener() {
                    @Override
                    public void checkPassword(boolean b) {
                        Toast.makeText(NFCHomeActivity.this, "秘钥校验结果" + b, Toast.LENGTH_SHORT).show();
                    }
                });
            });

        } else {
            // isDefaultPassword = getIntent().getBooleanExtra("isDefaultPassword", false);
            factory = getIntent().getBooleanExtra("isFactory", false);
            isWrite = getIntent().getBooleanExtra("isWrite", false);
            String staJson = getIntent().getStringExtra("data");
            data = JSON.parseObject(staJson);
            if (!factory && !isDebug) {
                mLanguage = data.getString("language");
            }
            //前端传过来的model和tagId 用来做校验
            checkModel = data.getString("model");
            checkTagId = data.getString("tagId");

            NFCModel.list.add(this);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            //将对话框的大小按屏幕大小的百分比设置
            Window window = this.getWindow();
            WindowManager m = window.getWindowManager();
            Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
            WindowManager.LayoutParams p = window.getAttributes(); // 3·获取对话框当前的参数值
            p.height = 10; // 高度设置为0
            p.width = 10; //宽0
            p.gravity = Gravity.RIGHT;
            window.setAttributes(p);
        }
        //初始化nfc
        mNfcAdapter = NFCInitTools.nfcCheck(this, mLanguage);
        if (mNfcAdapter != null) {
            createNfc(this);
        }
    }

    /**
     * @param intent
     */

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent == null) {
            return;
        }
        if (!factory) {
            if (RECEIVE_NFC_POSITION > 0 && !isDebug) {
                return;
            }
        }
        setIntent(intent);
        if (intent.getAction() == null) {
            factory = intent.getBooleanExtra("isFactory", false);
            isWrite = intent.getBooleanExtra("isWrite", false);
            String staJson = intent.getStringExtra("data");
            data = JSON.parseObject(staJson);
            if (!factory && !isDebug) {
                mLanguage = data.getString("language");
            }
            //前端传过来的model和tagId 用来做校验
            checkModel = data.getString("model");
            checkTagId = data.getString("tagId");
        }

        if (isWrite) {
            if (data != null) {
                acceptParams(data);
            } else {
                if (!isDebug) {
                    NFCModel.invokeError("写入数据为空");
                } else {
                    Log.i("", "writeTag2: 写入数据为空");
                }
            }
        } else {
            NFCInitTools.readTag(factory, true, mNfcAdapter, intent, mLanguage, new NFCListener() {

                @Override
                public void onReadId(String id) {
                }

                @Override
                public void onReadError() {
                    Log.i("===========", "onReadError: 失败失败");

                    if (!factory && !isDebug) {
                        runOnUiThread(() -> {
                            if (mLanguage != null && mLanguage.equals("zh")) {
                                if (recognition != null) {
                                    recognition.setContent("读取失败,请重新尝试");
                                }
                            } else {
                                if (recognition != null) {
                                    recognition.setContent("Read failure");
                                }
                            }
                            if (recognition != null) {
                                recognition.setGif(R.drawable.ic_shibie_fail);
                            }
                            new Handler().postDelayed(() -> {
                                if (recognition != null) {
                                    recognition.dismissDialog();
                                }
                                finish();
                            }, 1500);
                        });
                    }
                }

                @Override
                public void onReadBlocks(@NonNull List<String> array) {
                    RECEIVE_NFC_POSITION++;
                    //获取Tagid
                    JSONObject data = new JSONObject();
                    //获取TagId
                    String tagId = getTagId(array);
                    if (!TextUtils.isEmpty(tagId)) {
                        data.put("tagId", tagId);
                    }
                    //获取dd
                    String dd = getDD(array);
                    if (!TextUtils.isEmpty(dd)) {
                        data.put("dd", dd);
                    }

                    //获取产品的型号
                    String nFCModel = getNFCModel(array);

                    if (!TextUtils.isEmpty(nFCModel)) {
                        data.put("model", nFCModel);
                    }

                    //获取电量  36333046  C60D  43363044
                    String nfcQuantity = getNFCQuantity(array);
                    if (!TextUtils.isEmpty(nfcQuantity)) {
                        data.put("quantity", nfcQuantity);
                    }

                    //获取油量
                    String nfcOilMass = getNFCOilMass(array);
                    if (!TextUtils.isEmpty(nfcOilMass)) {
                        data.put("oilMass", nfcOilMass);
                    }

                    //获取当前等级
                    String nfcGrade = getNFCGrade(array);
                    if (!TextUtils.isEmpty(nfcGrade)) {
                        data.put("grade", nfcGrade);
                    }

                    //获取链接次数
                    String connectNum = getNFCConnectNum(array);
                    if (!TextUtils.isEmpty(connectNum)) {
                        data.put("connectNum", connectNum);
                    }

                    //获取健康度
                    String healthDegree = getNFCHealthDegree(array);
                    if (!TextUtils.isEmpty(healthDegree)) {
                        data.put("healthDegree", healthDegree);
                    }
                    //获取充电次数
                    String chargeTimes = getNFCChargeTimes(array);
                    if (!TextUtils.isEmpty(chargeTimes)) {
                        data.put("chargeTimes", chargeTimes);
                    }

                    List<List<String>> gradeList = getGradeCurve(array);
                    for (int i = 0; i < gradeList.size(); i++) {
                        data.put("grade" + (i + 1), gradeList.get(i));
                    }
                    if (gradeList.size() == 1) {
                        data.put("grade2", new ArrayList<>());
                        data.put("grade3", new ArrayList<>());
                    }
                    if (gradeList.size() == 2) {
                        data.put("grade3", new ArrayList<>());
                    }

                    if (isDebug) {
                        String jsonString = JSONObject.toJSONString(data);
                        Log.i("=====读取到json====", "onReadBlocks: " + jsonString);
                    } else {
                        NFCModel.invokeSuccess(data);
                    }
                    if (!factory && !isDebug) {
                        runOnUiThread(() -> {
                            if (mLanguage != null && mLanguage.equals("zh")) {
                                if (recognition != null) {
                                    recognition.setContent("读取成功");
                                }
                            } else {
                                if (recognition != null) {
                                    recognition.setContent("Read successfully");
                                }
                            }
                            if (recognition != null) {
                                recognition.setGif(R.drawable.ic_shibie_success);
                            }
                            new Handler().postDelayed(() -> {
                                if (recognition != null) {
                                    recognition.dismissDialog();
                                }
                                finish();
                            }, 1500);
                        });
                    }
                }
            });
        }

    }


    @Override
    protected void onResume() {
        super.onResume();

        if (!factory && !isDebug) {
            showDialog();
        }

        if (mNfcAdapter != null) {
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilter, mTechList);
        }
    }

    @Override
    protected void onPause() {
        if (mNfcAdapter != null) {
            mNfcAdapter.disableForegroundDispatch(this);
        }
        super.onPause();
    }

    /**
     *
     */
    public void createNfc(Context context) {
        int flag = 0;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
            flag = PendingIntent.FLAG_MUTABLE;
        }
        Intent intent = new Intent(context, context.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        mPendingIntent = PendingIntent.getActivity(context, 0, intent, flag);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("*/*");
            if (!isDebug) {
                if (mLanguage != null && mLanguage.equals("zh")) {
                    NFCModel.invokeSuccess("初始化成功", "initSuccess");
                } else {
                    NFCModel.invokeSuccess("initialization successful", "initSuccess");
                }
            }
        } catch (IntentFilter.MalformedMimeTypeException e) {
            if (!isDebug) {
                if (mLanguage != null && mLanguage.equals("zh")) {
                    NFCModel.invokeSuccess("初始化失败", "initSuccess");
                } else {
                    NFCModel.invokeSuccess("Initialization failed", "initSuccess");
                }
            }
        }
        IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        mIntentFilter = new IntentFilter[]{ndef, tag, tech};
        mTechList = new String[][]{new String[]{IsoDep.class.getName(), NfcA.class.getName(), NfcB.class.getName(), NfcF.class.getName(), NfcV.class.getName(), Ndef.class.getName(), NdefFormatable.class.getName(), MifareClassic.class.getName(), MifareUltralight.class.getName(),}};
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!isDebug) {
            NFCModel.list.remove(this);
        }
        EventBus.getDefault().unregister(this);
        recognition = null;
    }

    List<byte[]> byteList = new ArrayList<>();

    /**
     * 拿到参数进行写入
     */
    public void acceptParams(JSONObject options) {
        byteList.clear();
        String grade = options.getString("grade");
        if (!isWrite && !isDebug) {
            factory = options.getBoolean("isFactory");
        }
        //前端传过来的model和tagId 用来做校验
        checkModel = data.getString("model");
        checkTagId = data.getString("tagId");
        if (grade != null && !TextUtils.isEmpty(grade)) {
            //获取到等级,写入NFC
            byte[] arrayOf = NfcVUtil.instance.hexStringToBytes(NfcVUtil.instance.decimalToHexString(Integer.parseInt(grade)) + "000000");
            byteList.add(arrayOf);
        }

        List<String> grade1List = options.<List<String>>getObject("grade1", List.class);
        List<String> grade2List = options.<List<String>>getObject("grade2", List.class);
        List<String> grade3List = options.<List<String>>getObject("grade3", List.class);

        //拼接数据，不够的补0 ，十进制转十六进制
        List<Byte> list = new ArrayList<>();
        list.add((byte) 0X40);
        list.add((byte) 0X64);
        if (grade1List != null && grade1List.size() == 8) {
            //处理一条数据的情况，十进制转十六进制
            StringBuilder sb = new StringBuilder();
            for (String string : grade1List) {
                sb.append(NfcVUtil.instance.decimalToHexString(Integer.parseInt(string)));
            }
            byte[] bytes = NfcVUtil.instance.hexStringToByteArray(sb.toString());
            for (byte aByte : bytes) {
                list.add(aByte);
            }
        }
        if (grade2List != null && grade2List.size() == 8) {
            //处理一条数据的情况,十进制转十六进制
            StringBuilder sb = new StringBuilder();
            for (String string : grade2List) {
                sb.append(NfcVUtil.instance.decimalToHexString(Integer.parseInt(string)));
            }
            byte[] bytes = NfcVUtil.instance.hexStringToByteArray(sb.toString());
            for (byte aByte : bytes) {
                list.add(aByte);
            }
        }

        if (grade3List != null && grade3List.size() == 8) {
            //处理一条数据的情况,十进制转十六进制
            StringBuilder sb = new StringBuilder();
            for (String string : grade3List) {
                sb.append(NfcVUtil.instance.decimalToHexString(Integer.parseInt(string)));
            }
            byte[] bytes = NfcVUtil.instance.hexStringToByteArray(sb.toString());
            for (byte aByte : bytes) {
                list.add(aByte);
            }
        }
        list.add((byte) 0X00);
        list.add((byte) 0XFB);

        if (list.size() == 4) {
            list.clear();
        }

        int size = list.size();
        if (size != 0 && size != 12 && size != 20 && size != 28) {
            if (!isDebug) {
                NFCModel.invokeError("数据错误,请补全信息");
            }
            return;
        }

        //给缺失的档位补0
        if (size > 0 && size < 28) {
            for (int i = 0; i < 28 - size; i++) {
                list.add((byte) 0X00);
            }
        }
        //开始补上byte数组
        List<Byte> bytes = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            bytes.add(list.get(i));
            if ((i + 1) % 4 == 0) {
                Byte[] array = bytes.toArray(new Byte[0]);

                byte[] byteSm = new byte[array.length];
                for (int j = 0; j < array.length; j++) {
                    byteSm[j] = array[j];
                }
                byteList.add(byteSm);
                bytes = new ArrayList<>();
            }
        }


        startPosition = 20;
        successPosition = 1;
        //从20位 开始写入到27位
        //数据写入
        if (byteList.size() == 8) {
            //需要先写入功率
            writeTag(byteList.get(0), 27, new NFCListener() {
                @Override
                public void writeSuccess(boolean status) {
                    //开始写入功率曲线
                    ThreadTools.runOnMainThread(() -> {
                        byteList.remove(0);
                        writePowerCurve();
                    });
                }

                @Override
                public void onReadError() {
                    if (!factory && !isDebug) {
                        runOnUiThread(() -> {

                            if (mLanguage != null && mLanguage.equals("zh")) {
                                if (recognition != null) {
                                    recognition.setContent("写入失败");
                                }
                            } else {
                                if (recognition != null) {
                                    recognition.setContent("Write failure");
                                }
                            }
                            if (recognition != null) {
                                recognition.setGif(R.drawable.ic_shibie_fail);
                            }
                            new Handler().postDelayed(() -> {
                                if (recognition != null) {
                                    recognition.dismissDialog();
                                }
                                finish();
                            }, 1500);

                        });
                    }
                }
            });
        } else {
            //直接写入功率曲线
            if (!byteList.isEmpty()) {
                startPosition = 27;
                writePowerCurve();
            } else {
                NFCModel.invokeError("数据为空，写入失败");
            }
        }
    }

    private void writePowerCurve() {
        byte[] bytes1 = byteList.get(0);
        writeTag(bytes1, startPosition, new NFCListener() {
            @Override
            public void writeSuccess(boolean status) {
                if (!status) {
                    if (!isDebug) {
                        NFCModel.invokeError("写入失败");
                    } else {
                        Log.i("", "writeTag2: 写入失败");
                    }
                    onReadError();
                } else {
                    byteList.remove(0);
                    if (!byteList.isEmpty()) {
                        Log.i("", "writeTag2: 写入成功");
                        startPosition++;
                        byte[] bytes2 = byteList.get(0);
                        writeTag(bytes2, startPosition, this);
                    } else {
                        if (isDebug) {
                            Log.i("", "writeTag2: 写入成功");
                        } else {
                            if (mLanguage != null && mLanguage.equals("zh")) {
                                NFCModel.invokeSuccess("写入成功", "initSuccess");
                            } else {
                                NFCModel.invokeSuccess("The write succeeded", "initSuccess");
                            }
                        }
                        if (!factory && !isDebug) {
                            runOnUiThread(() -> {
                                if (mLanguage != null && mLanguage.equals("zh")) {
                                    if (recognition != null) {
                                        recognition.setContent("写入成功");
                                    }
                                } else {
                                    if (recognition != null) {
                                        recognition.setContent("Write successfully");
                                    }
                                }
                                if (recognition != null) {
                                    recognition.setGif(R.drawable.ic_shibie_success);
                                }
                                new Handler().postDelayed(() -> {
                                    if (recognition != null) {
                                        recognition.dismissDialog();
                                    }
                                    finish();
                                }, 1500);

                            });
                        }
                    }
                }
            }

            @Override
            public void onReadError() {
                if (!factory && !isDebug) {
                    runOnUiThread(() -> {
                        if (mLanguage != null && mLanguage.equals("zh")) {
                            if (recognition != null) {
                                recognition.setContent("写入失败");
                            }
                        } else {
                            if (recognition != null) {
                                recognition.setContent("Write failure");
                            }
                        }
                        if (recognition != null) {
                            recognition.setGif(R.drawable.ic_shibie_fail);
                        }
                        new Handler().postDelayed(() -> {
                            if (recognition != null) {
                                recognition.dismissDialog();
                            }
                            finish();
                        }, 1500);

                    });
                }
            }
        });
    }

    //开始写入位置
    int startPosition = 20;
    int successPosition = 0;

    private void writeTag(byte[] bytes, int position, NFCListener nfcListener) {
        NFCInitTools.writeTag2(bytes, position, getIntent(), mNfcAdapter, mLanguage, checkTagId, checkModel, nfcListener);
    }


    /**
     * 获取GradeCurve
     */
    private List<List<String>> getGradeCurve(List<String> array) {
        List<List<String>> gradeList = new ArrayList<>();

        StringBuilder sbGradeCurve = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i >= 20 && i <= 26) {
                sbGradeCurve.append(array.get(i));
            }
        }

        if (!sbGradeCurve.toString().contains("4064") || !sbGradeCurve.toString().contains("00fb")) {
            return new ArrayList<>();
        }

        String gradeCurve = sbGradeCurve.substring((sbGradeCurve.toString().indexOf("4064") + 4), (sbGradeCurve.toString().indexOf("00fb")));

        List<String> list = new ArrayList<>();
        StringBuilder temp = new StringBuilder();
        char[] charArray = gradeCurve.toCharArray();
        for (char c : charArray) {
            temp.append(c);
            if (temp.length() == 2) {
                list.add(temp.toString());
                temp = new StringBuilder();
            }
        }
        List<String> tempList = new ArrayList<>();
        for (String string : list) {
            tempList.add(NFCInitTools.hexToDecimal(string) + "");
            if (tempList.size() == 8) {
                gradeList.add(tempList);
                tempList = new ArrayList<>();
            }
        }

        return gradeList;

    }

    /**
     * 获取油量
     */
    private String getNFCOilMass(List<String> array) {
        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i == 18) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToAscii(dd.toString());
    }

    /**
     * 获取电量
     */
    private String getNFCQuantity(List<String> array) {
        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i == 17) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToAscii(dd.toString());
    }

    /**
     * 获取型号
     */
    private String getNFCModel(List<String> array) {
        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i == 28) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return dd.toString().substring(0, 4);
//        return NFCInitTools.hexToDecimal(dd.toString().substring(0, 4)) + "";
    }

    /**
     * 获取链接次数
     */
    private String getNFCConnectNum(List<String> array) {
        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i == 28) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToDecimal(dd.toString().substring(4, 8)) + "";
    }

    /**
     * 获取链接次数
     */
    private String getNFCHealthDegree(List<String> array) {
        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i == 29) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToDecimal(dd.toString().substring(0, 4)) + "";
    }

    /**
     * 获取链接次数
     */
    private String getNFCChargeTimes(List<String> array) {
        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i == 29) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToDecimal(dd.toString().substring(4, 8)) + "";
    }

    /**
     * 获取Tagid
     */
    private String getNFCGrade(List<String> array) {

        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i == 27) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToDecimal(dd.toString().substring(0, 2)) + "";
    }

    /**
     * 获取Tagid
     */
    private String getTagId(List<String> array) {

        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i >= 12 && i <= 15) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToAscii(dd.toString());
    }


    /**
     * 获取dd
     */
    private String getDD(List<String> array) {

        StringBuilder dd = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            if (i >= 17 && i <= 19) {
                dd.append(array.get(i));
            }
        }
        if (TextUtils.isEmpty(dd)) {
            return "";
        }
        return NFCInitTools.hexToAscii(dd.toString());
    }

    /**
     * 获取识别弹框
     */
    private void showDialog() {
        if (recognition == null) {
            recognition = CommonBasicDialog.newInstance();
        }
        recognition.setOutCancel(false);
        recognition.setDimAmout(0.6f);
        recognition.setShowBottom(true);
        recognition.setLayoutId(R.layout.dialog_nfc_recognition_layout);
        recognition.setActivity(this);
        recognition.setLanguage(mLanguage);
        recognition.showDialog(getFragmentManager());
    }
}
